$(document).ready(function() {
    validateInputsOnKeyUp();
});

var validateInputsOnKeyUp = function() {
    $('.required').keyup(function() {
//                    console.log($(this));
        ajaxValidation($(this));
    });
}

var ajaxValidation = function(object) {
    var $this = $(object);
    var param = $this.attr("name");
    console.log(param);
    var value = $this.val();
    console.log(value);
    var jqxhr = $.get("inputValidation.php", {'param': param, 'value': value}, function(data) {
//                    console.log(data);
        if (data) {
            validateRequiredInputs();
        } else {
            $this.addClass('failed');
        }
    });
}

var validateRequiredInputs = function() {
    var numberOfMissingInputs = 0;
    $('.required').each(function(index) {
        var $item = $(this);
        var itemValue = $item.val();

        if (itemValue.length) {
            $item.removeClass('failed');
        } else {
            $item.addClass('failed');
            numberOfMissingInputs++;
        }
    });
//                console.log(numberOfMissingInputs);
    var $submit = $('#btnSubmit');
    if (numberOfMissingInputs > 0) {
        $submit.attr('disabled', true);
    } else {
        $submit.removeAttr('disabled');
    }
}